package com.zjdiepu.www.manage.api.base;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.annotation.Add;
import com.zjdiepu.www.base.annotation.Update;
import com.zjdiepu.www.base.core.AbstractSysUser;
import com.zjdiepu.www.base.core.ApiResponse;
import com.zjdiepu.www.base.core.BaseException;
import com.zjdiepu.www.base.core.PageVo;
import com.zjdiepu.www.base.enums.type.OperateType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.common.util.CollectionInitUtil;
import com.zjdiepu.www.ibiz.base.IDpBaseDataDictionaryService;
import com.zjdiepu.www.ibiz.zpp.PublicService;
import com.zjdiepu.www.model.base.dto.DpBaseDataDictionary;
import com.zjdiepu.www.model.base.ext.DpBaseDataDictionaryExt;

/**
 * 所属公司：稻普控股集团
 * 项目名称：dpqc-parent-manager-dictionaries
 * 类名称：DataDictionaryController
 * 类描述：
 * 创建人：winby
 * 创建时间：2017年4月5日15:57:49
 * 修改人：winby
 * 修改时间：2017年4月5日15:57:49
 * 修改备注：
 *
 * @version V1.0
 */
@RestController
@RequestMapping(value= "DataDictionaryController", produces="application/json;charset=UTF-8")
@Api(tags = {"数据字典"}, produces="application/json;charset=UTF-8")
public class DataDictionaryController extends BaseController 
{
    @Resource
    private IDpBaseDataDictionaryService dataDictionaryService;
    
    @Resource
    private PublicService publicService;

    /**
     * @author winby <br/>
     * @Title: save  <br/>
     * @Description: 新增数据字典 <br/>
     * @param dpBaseDataDictionary
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "新增数据字典", response = Boolean.class, httpMethod="POST", tags={"新增"})
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public ApiResponse save(@Validated(Add.class)@ModelAttribute DpBaseDataDictionary dpBaseDataDictionary) {
        DpBaseDataDictionaryExt dpBaseDataDictionaryExt = new DpBaseDataDictionaryExt();
        dpBaseDataDictionaryExt.notDeleted();
        dpBaseDataDictionaryExt.setParentValue(dpBaseDataDictionary.getParentValue());
        dpBaseDataDictionaryExt.setValue(dpBaseDataDictionary.getValue());
       	int countValues = dataDictionaryService.countByExt(dpBaseDataDictionaryExt);
    	if(countValues > 0){
            return ApiResponse.fail(999, "字典值重复");
        }
    	dpBaseDataDictionaryExt.setValue(null);
    	dpBaseDataDictionaryExt.setName(dpBaseDataDictionary.getName());
        int countNames = dataDictionaryService.countByExt(dpBaseDataDictionaryExt);
    	if(countNames > 0){
            return ApiResponse.fail(999, "字典名称重复");
        }
    	
        AbstractSysUser currentUser = getCurrentUser();
    	dpBaseDataDictionary.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dpBaseDataDictionary.setModifierName(currentUser.getName());
        dataDictionaryService.saveUpdateAndDelete(dpBaseDataDictionary, OperateType.add);
        
        //查询数据字典
        publicService.getDictionary("查询数据字典");
        //查询数据字典
        
        return ApiResponse.success(true);
    }

    /**
     * @author winby <br/>
     * @Title: remove  <br/>
     * @Description: 删除数据字典<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "删除数据字典", response = Boolean.class, httpMethod="POST", tags={"删除"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query",  required = true, example="1")
    })
    @RequestMapping(value = "remove", method = RequestMethod.POST)
    public ApiResponse remove(String id) {
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
    	DpBaseDataDictionary dpBaseDataDictionary = new DpBaseDataDictionary();
    	dpBaseDataDictionary.setId(id);
    	AbstractSysUser currentUser = getCurrentUser();
        dpBaseDataDictionary.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
        dpBaseDataDictionary.setModifierName(currentUser.getName());
        dataDictionaryService.saveUpdateAndDelete(dpBaseDataDictionary, OperateType.delete);
        
        //查询数据字典
        publicService.getDictionary("查询数据字典");
        //查询数据字典
        
        return ApiResponse.success(true);
    }

    /**
     * @author winby <br/>
     * @Title: remove  <br/>
     * @Description: 启用数据字典<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "启用数据字典", response = Boolean.class, httpMethod="POST", tags={"启用"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "数据主键", paramType="query",  required = true, example="1")
    })
    @RequestMapping(value = "enable", method = RequestMethod.POST)
    public ApiResponse enable(String id) {
        if(StringUtils.isBlank(id)) {
            return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
        }
        DpBaseDataDictionary dpBaseDataDictionary = new DpBaseDataDictionary();
        dpBaseDataDictionary.setId(id);
        dpBaseDataDictionary.setDpStatus(1);
        AbstractSysUser currentUser = getCurrentUser();
        dpBaseDataDictionary.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
        dpBaseDataDictionary.setModifierName(currentUser.getName());
        dataDictionaryService.saveUpdateAndDelete(dpBaseDataDictionary, OperateType.edit);
        
        //查询数据字典
        publicService.getDictionary("查询数据字典");
        //查询数据字典
        
        return ApiResponse.success(true);
    }

    /**
     * @author winby <br/>
     * @Title: remove  <br/>
     * @Description: 禁用数据字典<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "禁用数据字典", response = Boolean.class, httpMethod="POST", tags={"禁用"})
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "数据主键", paramType="query",  required = true, example="1")
    })
    @RequestMapping(value = "disable", method = RequestMethod.POST)
    public ApiResponse disable(String id) {
        if(StringUtils.isBlank(id)) {
            return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
        }
        DpBaseDataDictionary dpBaseDataDictionary = new DpBaseDataDictionary();
        dpBaseDataDictionary.setId(id);
        dpBaseDataDictionary.setDpStatus(2);
        AbstractSysUser currentUser = getCurrentUser();
        dpBaseDataDictionary.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
        dpBaseDataDictionary.setModifierName(currentUser.getName());
        dataDictionaryService.saveUpdateAndDelete(dpBaseDataDictionary, OperateType.edit);
        
        //查询数据字典
        publicService.getDictionary("查询数据字典");
        //查询数据字典
        
        return ApiResponse.success(true);
    }

    /**
     * @author winby <br/>
     * @Title: modify  <br/>
     * @Description: 修改数据字典 <br/>
     * @param entity
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "修改数据字典", response = Boolean.class, httpMethod="POST", tags={"修改"})
    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public ApiResponse modify(@Validated(Update.class)@ModelAttribute DpBaseDataDictionary dpBaseDataDictionary) {
    	String id = dpBaseDataDictionary.getId();
    	DpBaseDataDictionaryExt dpBaseDataDictionaryExt = new DpBaseDataDictionaryExt();
    	dpBaseDataDictionaryExt.notDeleted();
    	dpBaseDataDictionaryExt.setParentValue(dpBaseDataDictionary.getParentValue());
    	dpBaseDataDictionaryExt.setValue(dpBaseDataDictionary.getValue());
    	int countValues = dataDictionaryService.countByExt(dpBaseDataDictionaryExt);
    	if(countValues > 1){
    		return ApiResponse.fail(999, "字典值重复");
    	} else if(countValues == 1) {
    		dpBaseDataDictionaryExt.setRows(1);
    		List<DpBaseDataDictionary> selectForValues = dataDictionaryService.selectByExt(dpBaseDataDictionaryExt);
    		if(CollectionUtils.isNotEmpty(selectForValues)) {
    			if(!selectForValues.get(0).getId().equals(id)){
    				return ApiResponse.fail(999, "字典值重复");
    			}
    		}
    	}
    	
    	dpBaseDataDictionaryExt.setValue(null);
    	dpBaseDataDictionaryExt.withOutPage();
    	dpBaseDataDictionaryExt.setName(dpBaseDataDictionary.getName());
        int countNames = dataDictionaryService.countByExt(dpBaseDataDictionaryExt);
    	if(countNames > 1){
            return ApiResponse.fail(999, "字典名称重复");
        } else if(countNames == 1){
        	dpBaseDataDictionaryExt.setRows(1);
    		List<DpBaseDataDictionary> selectForNames = dataDictionaryService.selectByExt(dpBaseDataDictionaryExt);
    		if(CollectionUtils.isNotEmpty(selectForNames)) {
    			if(!selectForNames.get(0).getId().equals(id)){
    				return ApiResponse.fail(999, "字典名称重复");
    			}
    		}
        }
    	
    	AbstractSysUser currentUser = getCurrentUser();
    	dpBaseDataDictionary.setModifier(currentUser.getId()); //统一使用修改人和修改人名称来初始化
    	dpBaseDataDictionary.setModifierName(currentUser.getName());
        dataDictionaryService.saveUpdateAndDelete(dpBaseDataDictionary, OperateType.edit);
        
        //查询数据字典
        publicService.getDictionary("查询数据字典");
        //查询数据字典
        
        return ApiResponse.success(true);
    }

    /**
     * @author winby <br/>
     * @Title: getById  <br/>
     * @Description: 主键查询数据字典<br/>
     * @param id
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "主键查询数据字典", response = DpBaseDataDictionary.class, httpMethod="POST", tags={"主键查询"})
    @ApiImplicitParams({
        @ApiImplicitParam(name = "id", value = "数据主键", paramType="query", required = true, example="1")
    })
    @RequestMapping(value = "getById", method = RequestMethod.POST)
    public ApiResponse getById(String id){
    	if(StringUtils.isBlank(id)) {
    		return ApiResponse.fail(ResultFlagType.PARAM_EMPTY);
    	}
       DpBaseDataDictionary dpBaseDataDictionary = dataDictionaryService.selectById(id);
       if(null == dpBaseDataDictionary) {
    	   throw new BaseException(ResultFlagType.MISSING.getCode().toString(),ResultFlagType.MISSING.getValue());
       }
       return ApiResponse.success(dpBaseDataDictionary);
    }
    
    /**
     * @author winby <br/>
     * @Title: list  <br/>
     * @Description: 条件查询数据字典 <br/>
     * @param dpBaseDataDictionaryExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "条件查询数据字典",response = DpBaseDataDictionary.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "list", method = RequestMethod.POST)
    public ApiResponse list(@ModelAttribute DpBaseDataDictionaryExt dpBaseDataDictionaryExt){
        dpBaseDataDictionaryExt.withOutPage();
        dpBaseDataDictionaryExt.notDeleted();
        dpBaseDataDictionaryExt.setOrderSql("`CREATED`");
        PageVo page = getPage(dpBaseDataDictionaryExt);
        int records = dataDictionaryService.countByExt(dpBaseDataDictionaryExt);
        page.setRecords(records);
        if (records > 0) {
            List<DpBaseDataDictionary> dpBaseDataDictionaries = dataDictionaryService.selectByExt(dpBaseDataDictionaryExt);
            page.setRows(dpBaseDataDictionaries);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }

    /**
     * @author winby <br/>
     * @Title: list  <br/>
     * @Description: 条件查询数据字典 <br/>
     * @param dpBaseDataDictionaryExt
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "条件查询数据字典",response = DpBaseDataDictionary.class, httpMethod="POST", tags={"列表"})
    @RequestMapping(value = "getEnableList", method = RequestMethod.POST)
    public ApiResponse getEnableList(@ModelAttribute DpBaseDataDictionaryExt dpBaseDataDictionaryExt){
        dpBaseDataDictionaryExt.withOutPage();
        dpBaseDataDictionaryExt.setBaseWhere();
        dpBaseDataDictionaryExt.setOrderSql("`CREATED`");
        PageVo page = getPage(dpBaseDataDictionaryExt);
        int records = dataDictionaryService.countByExt(dpBaseDataDictionaryExt);
        page.setRecords(records);
        if (records > 0) {
            List<DpBaseDataDictionary> dpBaseDataDictionaries = dataDictionaryService.selectByExt(dpBaseDataDictionaryExt);
            page.setRows(dpBaseDataDictionaries);
        } else {
            page.setRows(CollectionInitUtil.emptyList());
        }
        return ApiResponse.success(page);
    }
}
